Type: concept
Confidence: 0.90
Created: 2026-04-16
Updated: 2026-04-16
Tags: UrhoXLuaIO文件系统游戏引擎游戏开发

UrhoX IO系统API

概述

UrhoX Lua IO 系统提供文件读写、虚拟文件系统(VFS)打包、内存缓冲区、命名管道等能力,核心类均继承自 Object 或 Deserializer/Serializer 接口。

关键内容

File — 文件读写

File : Object 支持普通文件和 PackageFile 内资源的读写,以 FileModeFILE_READ / FILE_WRITE / FILE_READWRITE)控制模式。

常用操作:

local f = File("data/config.bin", FILE_READ)
local value = f:ReadInt()
f:Close()

local out = File("output.txt", FILE_WRITE)
out:WriteLine("hello world")
out:Close()

关键方法:OpenCloseFlushSeek/SeekRelativeRead(返回 VectorBuffer)、IsEofIsPackaged

属性(均只读):modeopenpackagednamechecksumpositionsizeeof

FileSystem — 文件系统操作

FileSystem : Object 暴露为全局子系统 fileSystem,提供目录管理、文件操作、系统命令执行等功能。

-- 检查文件是否存在
if fileSystem:FileExists("saves/slot1.dat") then ... end

-- 创建目录
fileSystem:CreateDir("saves/")

-- 扫描目录
local files = fileSystem:ScanDir("assets/", "*.png", SCAN_FILES, false)

常用方法:FileExistsDirExistsCreateDirCopyRenameDeleteScanDirGetCurrentDirGetProgramDirGetUserDocumentsDirGetAppPreferencesDirSystemCommand/SystemRun(同步执行外部命令)。

⚠️ 引擎沙箱限制:不能用标准 Lua io 库,必须用 FileFileSystem。相对路径以沙箱工作目录为根。

VectorBuffer — 内存缓冲区

VectorBuffer 同时实现 Serializer 和 Deserializer,用于在内存中构建或解析二进制数据块,可与 File 互转:

local buf = VectorBuffer()
buf:WriteInt(42)
buf:WriteString("test")
-- 重置游标
buf:Seek(0)
local n = buf:ReadInt()  -- 42

NamedPipe — 命名管道

NamedPipe : Object 支持进程间通信(IPC),以服务端/客户端模式打开:

local pipe = NamedPipe("mypipe", true)  -- isServer=true
if pipe:IsOpen() then
    pipe:WriteString("data")
end

属性:nameopeneof(均只读)。

PackageFile — VFS 打包资源

PackageFile : Object 表示引擎打包格式(.pak),用于将多个文件打包为单一资源包,支持压缩。

local pkg = PackageFile("data.pak")
if pkg:Exists("textures/hero.png") then
    local entry = pkg:GetEntry("textures/hero.png")
    -- entry.offset, entry.size, entry.checksum
end

属性:namenumFilestotalSizetotalDataSizechecksumcompressed

PackageEntry 为轻量值类型,包含 offsetsizechecksum

来源

相关